package com.corticon.client.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataValidationUtils {
	private final String DefaultDateFormat = "mm/dd/yyyy";
	private final String MinError = "The value you entered must no less than ";
	private final String MaxError = "The value you entered must no more than ";
	private final String LessMinError = "The value you entered must no more than ";
	private final String MoreMaxError = " and no less than ";
	private final String DateError = "Please enter a date in the format MM/DD/YYYY";
	private final String DatePix = "/";

	public String CheckDate(String InputText, String DateFormat, String maxValue, String minValue) {
		String errorMessage = DateError;
		DateFormat = DateFormat.equals("") ? DefaultDateFormat : DateFormat;
		DateFormat dateFormat = new SimpleDateFormat(DateFormat);
		Date d = null;
		boolean checkflag = false;
		String[] check = InputText.split("/");
		String newStr1 = "", newStr2 = "", newStr3 = "", newStr = "";
		try {
			for (int m = 0; m < check.length; m++) {
				String checkThis = check[m];
				if (m == 0) {
					if (checkThis.length() > 2 || Integer.valueOf(checkThis) > 12) {
						checkflag = true;
					}
					newStr2 = checkThis;
				}
				if (m == 1) {
					if (checkThis.length() > 2 || Integer.valueOf(checkThis) > 31) {
						checkflag = true;
					}
					newStr3 = checkThis;
				}
				if (m == 2) {
					if (checkThis.length() > 4) {
						checkflag = true;
					}
					newStr1 = checkThis;
				}
			}
			newStr = newStr1 + "/" + newStr2 + "/" + newStr3;
			Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\/\\/\\s]?((((0?" + "[13578])|(1[02]))[\\/\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))"
					+ "|(((0?[469])|(11))[\\/\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|" + "(0?2[\\/\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12"
					+ "35679])|([13579][01345789]))[\\/\\/\\s]?((((0?[13578])|(1[02]))" + "[\\/\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))"
					+ "[\\/\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\/\\/\\s]?((0?[" + "1-9])|(1[0-9])|(2[0-8]))))))");
			if (p.matcher(newStr).matches() == false) {
				checkflag = true;
			}
			if (checkflag) {
				return errorMessage;
			}
			try {
				dateFormat.setLenient(false);
				d = dateFormat.parse(InputText);
			} catch (Exception e) {
				return errorMessage;
			}

			Date inputDate = dateFormat.parse(InputText);
			if (!minValue.equals("") && !maxValue.equals("")) {
				Date maxValueDate = dateFormat.parse(maxValue);
				Date minValueDate = dateFormat.parse(minValue);
				if (inputDate.after(maxValueDate) || inputDate.before(minValueDate)) {
					errorMessage = LessMinError + maxValue + MoreMaxError + minValue;
					return errorMessage;
				} else {
					if (!maxValue.equals("")) {
						if (inputDate.after(maxValueDate)) {
							errorMessage = MaxError + maxValue;
							return errorMessage;
						}
					}
					if (!minValue.equals("")) {
						if (inputDate.before(minValueDate)) {
							errorMessage = MinError + minValue;
							return errorMessage;
						}
					}
				}
			} else {
				if (!maxValue.equals("")) {
					Date maxValueDate = dateFormat.parse(maxValue);
					if (inputDate.after(maxValueDate)) {
						errorMessage = MaxError + maxValue;
						return errorMessage;
					}
				}
				if (!minValue.equals("")) {
					Date minValueDate = dateFormat.parse(minValue);
					if (inputDate.before(minValueDate)) {
						errorMessage = MinError + minValue;
						return errorMessage;
					}
				}
			}
		} catch (Exception e) {
			return errorMessage;
		}
		return "";
	}

	public String CheckInteger(String InputText, String maxValue, String minValue) {
		String errorMessage = "";
		try {

		} catch (Exception e) {
			return errorMessage;
		}
		return errorMessage;
	}

	public String CheckFloat(String InputText, String maxValue, String minValue) {
		String errorMessage = "";
		try {

		} catch (Exception e) {
			return errorMessage;
		}
		return errorMessage;
	}
}
